package com.hunt.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hunt.dao.FinancialDataMapper;
import com.hunt.model.dto.FinancialData;
import com.hunt.model.dto.FinancialSimpleDataInfoPO;
import com.hunt.model.entity.StockBaseMessage;
import com.hunt.service.FinancialDataServiceNew;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.Jedis;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

@Service
@Transactional
public class FinancialDataServiceNewImpl implements FinancialDataServiceNew {
    @Autowired
    private FinancialDataMapper financialDataMapper;
    @Resource
    private RedisTemplate<String, Object> stringredisTemplate;

    @Override
    public int insertdata(String data) {
        FinancialData financialData = JSON.parseObject(data, FinancialData.class);
        try {
            if (financialData.getThs_is_shutdown().equals("0")) {
                String shutdowncode = financialData.getThs_thscode_stock();
                stringredisTemplate.boundHashOps("Real_time_Stock_Data").delete(shutdowncode.toLowerCase());
            }
            Date da = new Date();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            financialData.setUpdate_time(formatter.format(da));
            financialDataMapper.saveOrUpdateNew(financialData);
            //向mysql 里存
            Map<String, String> map = new ConcurrentHashMap<>();
            FinancialSimpleDataInfoPO financialSimpleDataInfo = new FinancialSimpleDataInfoPO(financialData);
            map.put(financialSimpleDataInfo.getTs_code(), JSON.toJSONString(financialSimpleDataInfo));
            stringredisTemplate.boundHashOps("Stock_Base_Message").putAll(map);
        } catch (Exception e) {
            return 0;
        }
        return 1;
    }

    @Override
    public int insertblock(String data) {
        try {
            JSONObject j1 = JSONObject.parseObject(data);
            Map<String, String> map = new HashMap<>();
            map.put(j1.getString("blockCode"), j1.toJSONString());
            stringredisTemplate.boundHashOps("Block_Info").putAll(map);
        } catch (Exception e) {
            return 0;
        }
        return 1;
    }

    @Override
    public int insertlimit(String data) {
        try {
            JSONObject j1 = JSONObject.parseObject(data);
            Map<String, String> map = new HashMap<>();
            map.put(j1.getString("thscode"), j1.toJSONString());
            stringredisTemplate.boundHashOps("Price_Limit").putAll(map);
        } catch (Exception e) {
            return 0;
        }
        return 1;
    }

    @Override
    public int insertstockbasemessage(String data) {
        try {
            Map<String, String>map=new HashMap<>();
            List<StockBaseMessage> list=JSON.parseArray(data, StockBaseMessage.class);
            for(StockBaseMessage s:list){
              map.put(s.getTs_code(),JSON.toJSONString(s));
            }
            stringredisTemplate.opsForHash().putAll("Stock_Base_Message",map);
        } catch (Exception e) {
            return 0;
        }
        return 1;
    }
}
